<script setup lang="ts">
import { PropType } from 'vue'
import { pjtUser } from '../pages/a_ProjectMine/types'

const avatarColor = (userName: string) => {
  const colors = ['primary', '#FFD43A', '#ADFF00', '#262824', 'danger']
  const index = userName.charCodeAt(0) % colors.length
  return colors[index]
}

defineProps({
  user: {
    type: Object as PropType<pjtUser>,
    required: true,
  },
  size: {
    type: String,
    default: 'medium',
  },
})

const isUrl = (avatar: string) => {
  return true;
  // return avatar.startsWith('http') || avatar.startsWith('blob:')
}
// const isUrl = (avatar) => {
//   // 确保avatar是一个字符串
//   if (typeof avatar !== 'string') {
//     return false
//   }
//   // 使用传统方法检查是否以'http'或'blob:'开头
//   return avatar.indexOf('http') === 0 || avatar.indexOf('blob:') === 0
// }
</script>

<template>
  <VaAvatar
    :size="size"
    :src="isUrl(user.photo) ? user.photo : ''"
    :fallback-text="user.name[0]"
    :color="avatarColor(user.name)"
  />
</template>
